package com.awei.study.dataStructure.link;

import java.util.Iterator;
import java.util.function.Consumer;

public class SingleLinkList implements Iterable<Integer> {
    private Node head = null;   //头指针

    @Override
    public Iterator<Integer> iterator() {   //用于争抢for循环
        return new IntegerIterator();
    }

    private class IntegerIterator implements Iterator<Integer> {
        Node p = head;

        @Override
        public boolean hasNext() {
            return p != null;
        }

        @Override
        public Integer next() {
            int v = p.value;
            p = p.next;
            return v;
        }
    }

    private static class Node {
        int value;
        Node next;

        public Node(int value, Node node) {
            this.value = value;
            this.next = node;
        }
    }

    public void addFirst(int value) {   //头部添加元素
        head = new Node(value, head);
    }

    public void loop() {    //while遍历
        Node node = head;
        while (node != null) {
            System.out.println(node.value);
            node = node.next;
        }
    }

    public void loop1() {    //for循环 遍历
        for (Node node = head; node != null; node = node.next) {
            System.out.println(node.value);
        }
    }

    public void loopConsumer(Consumer<Integer> consumer) {  //调用方设置遍历方式
        Node p = head;
        while (p != null) {
            consumer.accept(p.value);
            p = p.next;
        }
    }

    private Node findLast() {
        if (head == null) {
            return null;
        }
        Node p = head;
        while (p.next != null) {
            p = p.next;
        }
        return p;
    }

    public void addLast(int value) {
        Node last = findLast();
        if (last == null) {
            addFirst(value);
            return;
        }
        last.next = new Node(value, null);
    }

    private Node findNode(int index) {
        int i = 0;
        for (Node p = head; p != null; p = p.next, i++) {
            if (i == index) {
                return p;
            }
        }
        return null;
    }

    public Node get(int index) {
        Node node = findNode(index);
        if (node == null) {
            throw getIllegalArgumentException(index);
        }
        return node;
    }

    public void insert(int index, int value) {
        if (index == 0) {
            addFirst(value);
            return;
        }
        Node p = findNode(index - 1);
        if (p == null) {
            throw getIllegalArgumentException(index);
        }
        p.next = new Node(value,p.next);
    }

    public void removeFirst() {
        if(head == null) {
            throw getIllegalArgumentException(0);
        }
        head = head.next;
    }

    private IllegalArgumentException getIllegalArgumentException(int i) {
        return new IllegalArgumentException(String.format("index [%d] 不合法", i));
    }

    public void remove(int index) {
        if(index == 0) {
            removeFirst();
            return;
        }
        Node pre = findNode(index - 1);
        if(pre == null) {
            throw getIllegalArgumentException(index);
        }
        Node remove = pre.next;
        if(remove == null) {
            throw getIllegalArgumentException(index);
        }
        pre.next =remove.next;
    }


    public static void main(String[] args) {
        SingleLinkList n = new SingleLinkList();
        n.addLast(1);
        n.addLast(2);
        n.addLast(3);
        n.addLast(4);
//        n.insert(4,5);
        n.remove(6);
//        Node node = n.get(10);
//        System.out.println(node.value);
//        Assertions.assertIterableEquals(Arrays.asList(4,3,2,1,5),n);
        n.loop1();
//        n.loopConsumer(value->{
//            System.out.println(value);
//        });
//        for (Integer integer : n) {
//            System.out.println(integer);
//        }
    }

}
